<?php
/**
 * @author yuanyitian
 * @date 2024/8/26 18:45
 */

namespace api\admin;

class task
{
    //任务分页列表
    public function getList($data)
    {
        $con = con();
        if (isset($data['user_id'])) {
            $user_id = (int)$data['user_id'];
        } else {
            $user_id = 0;
        }
        $type = (int)$data['type'];//类型->0:所有任务，1：本人的所有任务，2：处理人能看到的任务，3：验证人能看到的任务
        $status = (int)$data['status'];
        $page = (int)$data['page'];
        $size = (int)$data['size'];
        $start = ($page - 1) * $size;
        $start_time = strtotime($data['start_time']);
        $end_time = strtotime($data['end_time']);

        $where = ' where is_delete=1 ';
        $where .= " and create_time between $start_time and $end_time";
        switch ($type) {
            case 0://所有任务
                break;
            case 1://本人任务
                if ($user_id != 0) {
                    $where .= " and up_user_id=$user_id";
                }
                break;
            case 2://处理人任务
                if ($user_id != 0) {
                    $where .= " and deal_user_id=$user_id";
                }
                break;
            case 3://验证人任务
                if ($user_id != 0) {
                    $where .= " and check_user_id=$user_id";
                }
                break;
            default:
                mysqli_close($con);
                return out_error('非法访问！');
                break;
        }

        if ($status != 100) {
            $where .= " and status=$status";
        }

        $limit = " order by update_time DESC limit $start,$size";

        $sql = "select * from task" . $where;
        $data = mysqli_query($con, $sql . $limit);
        $data = mysqli_fetch_all($data, true);
        $total = mysqli_query($con, $sql);
        $total = mysqli_num_rows($total);

        if (count($data) != 0) {
            $user_ids = array_merge(array_column($data, 'up_user_id'), array_column($data, 'deal_user_id'), array_column($data, 'check_user_id'));
            $a = $user_ids;
            for ($i = 0; $i < count($a); $i++) {
                if (empty($a[$i])) {
                    unset($user_ids[$i]);
                }
            }
            sort($user_ids);
            $user_ids = implode(',', $user_ids);
            $user = mysqli_query($con, "select * from user where id in ($user_ids)");
            $user = mysqli_fetch_all($user, true);
            for ($i = 0; $i < count($data); $i++) {
                $data[$i]['up_user_name'] = '';
                $data[$i]['up_user_phone'] = '';
                $data[$i]['deal_user_name'] = '';
                $data[$i]['deal_user_phone'] = '';
                $data[$i]['check_user_name'] = '';
                $data[$i]['check_user_phone'] = '';
                if (!empty($data[$i]['img_url'])) {//任务图片集合
                    $data[$i]['img_url'] = explode(',', $data[$i]['img_url']);
                } else {
                    $data[$i]['img_url'] = [];
                }
                if (!empty($data[$i]['deal_img'])) {//处理反馈图片集合
                    $data[$i]['deal_img'] = explode(',', $data[$i]['deal_img']);
                } else {
                    $data[$i]['deal_img'] = [];
                }
                if (!empty($data[$i]['pre_time'])) {//预约时间
                    $data[$i]['pre_time'] = date('Y-m-d H:i:s', $data[$i]['pre_time']);
                } else {
                    $data[$i]['pre_time'] = '';
                }
                if (!empty($data[$i]['band_time'])) {//领用时间
                    $data[$i]['band_time'] = date('Y-m-d H:i:s', $data[$i]['band_time']);
                } else {
                    $data[$i]['band_time'] = '';
                }
                if (!empty($data[$i]['server_time'])) {//上门服务时间
                    $data[$i]['server_time'] = date('Y-m-d H:i:s', $data[$i]['server_time']);
                } else {
                    $data[$i]['server_time'] = '';
                }
                if (!empty($data[$i]['check_time'])) {//验证时间
                    $data[$i]['check_time'] = date('Y-m-d H:i:s', $data[$i]['check_time']);
                } else {
                    $data[$i]['check_time'] = '';
                }
                if (!empty($data[$i]['cancel_time'])) {//取消时间
                    $data[$i]['cancel_time'] = date('Y-m-d H:i:s', $data[$i]['cancel_time']);
                } else {
                    $data[$i]['cancel_time'] = '';
                }
                if (!empty($data[$i]['create_time'])) {//创建时间
                    $data[$i]['create_time'] = date('Y-m-d H:i:s', $data[$i]['create_time']);
                } else {
                    $data[$i]['create_time'] = '';
                }
                if (!empty($data[$i]['update_time'])) {//修改时间
                    $data[$i]['update_time'] = date('Y-m-d H:i:s', $data[$i]['update_time']);
                } else {
                    $data[$i]['update_time'] = '';
                }
                if (empty($data[$i]['check_remark'])) {//验证评价
                    $data[$i]['check_remark'] = '';
                }
                for ($o = 0; $o < count($user); $o++) {
                    if ($data[$i]['up_user_id'] == $user[$o]['id']) {//发布人
                        $data[$i]['up_user_name'] = $user[$o]['name'];
                        $data[$i]['up_user_phone'] = $user[$o]['phone'];
                    }
                    if ($data[$i]['deal_user_id'] == $user[$o]['id']) {//执行人
                        $data[$i]['deal_user_name'] = $user[$o]['name'];
                        $data[$i]['deal_user_phone'] = $user[$o]['phone'];
                    }
                    if ($data[$i]['check_user_id'] == $user[$o]['id']) {//验证人
                        $data[$i]['check_user_name'] = $user[$o]['name'];
                        $data[$i]['check_user_phone'] = $user[$o]['phone'];
                    }
                }
            }
        }
        mysqli_close($con);
        return out_right([
            'data' => $data,
            'page' => $page,
            'size' => $size,
            'total' => $total,
            'total_page' => ceil($total / $size)
        ]);
    }
}